// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// p [author] rhyme_lph
// t [email]  rhymelph@gmail.com
// s [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/add_form.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/number_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/leave/leave_adjustment_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "leaveAdjustmentPage")
class LeaveAdjustmentPage extends StatefulWidget {
  @override
  _LeaveAdjustmentPageState createState() => _LeaveAdjustmentPageState();
}

class _LeaveAdjustmentPageState extends State<LeaveAdjustmentPage>
    with PagePresenter<LeaveAdjustmentPage, LeaveAdjustmentProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.leave_adjustment, actions: [
        NormalSaveTextButton(
          onTap: provider.onSave,
        )
      ]);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            listBuilder<Tuple2<String, bool>>(
              selector: (_, __) => Tuple2(
                  __.selectEmployeeList?.map((e) => e.name)?.join(','),
                  __.isAll),
              builder: (BuildContext context, Tuple2<String, bool> value,
                      Widget child) =>
                  ValueSelectForm(
                title: S.current.select_employee,
                value:
                    value.item2 == true ? S.current.all_employee : value.item1,
                onTap: provider.onSelectEmployee,
              ),
            ),
            listBuilder<int>(
              selector: (_, __) => __.leaveData.length,
              builder: (BuildContext context, int value, Widget child) =>
                  NormalFormWrapper(
                hasTopBorder: true,
                hasBottomBorder: value > 0,
                topPadding: getDp(15),
                bottomPadding: getDp(15),
                leftPadding: getDp(35),
                child: Text(
                  S.current.adjustments_days,
                  style: TextStyle(
                    color: aceBlackColor,
                    fontSize: getSp(16),
                    fontWeight: FontWeight.w700,
                    height: 14 / 16,
                  ),
                ),
              ),
            ),
            listBuilder<int>(
              selector: (_, __) => __.leaveData.length,
              builder: (BuildContext context, int value, Widget child) =>
                  Column(
                children:
                    List.generate(value, (index) => _buildNumberInput(index))
                        .toList(),
              ),
            ),
            AddForm(
              title: S.current.add_leave_type,
              onTap: provider.onAddLeaveType,
            ),
          ],
        ),
      );

  Widget _buildNumberInput(int index) => PressedOpacityButton(
        onPressed: () => provider.onChangeLeaveType(provider.leaveData[index]),
        child: NumberForm(
          titleWidget: listBuilder<Tuple2<String, int>>(
            selector: (_, __) => Tuple2(
                Config.settingData.leaveType
                        .firstWhere(
                            (element) =>
                                element.key == provider.leaveData[index].name,
                            orElse: () => null)
                        ?.name
                        ?.replaceAll('(Days)', '') ??
                    '',
                provider.leaveData[index].amount),
            builder: (BuildContext context, Tuple2<String, int> value,
                    Widget child) =>
                Text.rich(
              TextSpan(
                  children: [
                    TextSpan(
                        text: value.item1,
                        style: TextStyle(
                          color: aceDarkGrayColor,
                          fontWeight: FontWeight.w700,
                          fontSize: 16,
                        )),
                    if (value.item2 != 0)
                      TextSpan(
                          text:
                              ' ${(value.item2 ?? 0) > 0 ? '+' : ''}${value.item2 ?? ''}'),
                  ],
                  style: TextStyle(
                      color: FormEditStatusInheritedWidget.ofEnable(context)
                          ? aceBlueColor
                          : aceBlackColor,
                      fontSize: getSp(16),
                      fontWeight: FontWeight.w700,
                      height: 16 / 14,
                      fontFamily: "DinRoundPro")),
            ),
          ),
          inputWidth: getDp(65),
          initialNumber: provider.leaveData[index].amount,
          hasBottomBorder: true,
          canZero: true,
          canNavigate: true,
          disableHideButton: true,
          onNumberCallback: (value) =>
              provider.onChangeAnnualLeave(provider.leaveData[index], value),
        ),
      );
}
